#
# $Id: 78abb016d67be05dd16d82785494f8f362f111b6 $
#

include ../../Make.inc

SERVER_SRCS	= acct.c auth.c client.c conffile.c crypt.c exec.c files.c \
		  listen.c log.c mainconfig.c modules.c modcall.c \
		  radiusd.c stats.c soh.c \
		  session.c threads.c util.c valuepair.c version.c  \
		  xlat.c event.c realms.c evaluate.c vmps.c detail.c

SERVER_OBJS	+= $(SERVER_SRCS:.c=.lo)

INCLUDES	= ../include/autoconf.h ../include/conf.h    \
		  ../include/libradius.h ../include/radius.h \
		  ../include/radiusd.h ../include/ident.h ../include/stats.h

CFLAGS		+= -I$(top_builddir)/src
CFLAGS		+= -DHOSTINFO=\"${HOSTINFO}\"
CFLAGS		+= -DRADIUSD_VERSION=\"${RADIUSD_VERSION}\"
CFLAGS		+= -DRADIUSD_VERSION_STRING=\"${RADIUSD_VERSION_STRING}\"
ifdef RADIUSD_VERSION_COMMIT
CFLAGS		+= -DRADIUSD_VERSION_COMMIT=\"${RADIUSD_VERSION_COMMIT}\"
endif
CFLAGS		+= $(SNMP_INCLUDE)
VFLAGS		= -DRADIUSD_MAJOR_VERSION=$(RADIUSD_MAJOR_VERSION)
VFLAGS		+= -DRADIUSD_MINOR_VERSION=$(RADIUSD_MINOR_VERSION)
VFLAGS		+= -DRADIUSD_INCRM_VERSION=$(RADIUSD_INCRM_VERSION)
MODULE_LIBS	= $(STATIC_MODULES)
BINARIES	= radiusd$(EXEEXT) radwho$(EXEEXT) radclient$(EXEEXT) radmin$(EXEEXT) radconf2xml$(EXEEXT)

#
#  The RADIUS sniffer
#
PCAP_LIBS	= @PCAP_LIBS@
ifneq ($(PCAP_LIBS),)
BINARIES	+= radsniff$(EXEEXT)
endif

#
#  Not using shared libraries, add in ALL known static modules
#  at build time.
#
ifneq ($(USE_SHARED_LIBS),yes)
#
#  For static linking...
#
LINK_MODE	= -static -all-static

SUB_MODULES += rlm_eap_md5 rlm_eap_leap rlm_eap_tls rlm_eap_ttls rlm_eap_sim
SUB_MODULES += rlm_eap_peap rlm_eap_mschapv2 rlm_eap_gtc rlm_eap_ikev2
SUB_MODULES += rlm_sql_db2 rlm_sql_iodbc rlm_sql_mysql
SUB_MODULES += rlm_sql_oracle rlm_sql_postgresql rlm_sql_sybase rlm_sql_unixodbc
LIBS        += $(shell test -f ../modules/rlm_eap/libeap/$(LIBPREFIX)freeradius-eap.la && echo ../modules/rlm_eap/libeap/$(LIBPREFIX)freeradius-eap.la)

MODULE_LIBS	+= $(shell for x in $(MODULES);do test -f ../modules/$$x/$$x.la && echo -dlpreopen ../modules/$$x/$$x.la;done)
MODULE_LIBS	+= $(shell for x in $(SUB_MODULES);do test -f ../modules/*/types/$$x/$$x.la && echo -dlpreopen ../modules/*/types/$$x/$$x.la;done)
MODULE_LIBS	+= $(shell for x in $(SUB_MODULES);do test -f ../modules/*/drivers/$$x/$$x.la && echo -dlpreopen ../modules/*/drivers/$$x/$$x.la;done)
MODULE_OBJS     += $(shell for x in $(MODULES);do test -f ../modules/$$x/$$x.la && echo ../modules/$$x/$$x.la;done)
MODULE_OBJS     += $(shell for x in $(SUB_MODULES);do test -f ../modules/*/types/$$x/$$x.la && echo ../modules/*/types/$$x/$$x.la;done)
MODULE_OBJS     += $(shell for x in $(SUB_MODULES);do test -f ../modules/*/drivers/$$x/$$x.la && echo ../modules/*/drivers/$$x/$$x.la;done)
endif

.PHONY: all clean install reconfig
all: $(BINARIES)

$(SERVER_OBJS): $(INCLUDES)

radiusd$(EXEEXT): $(SERVER_OBJS) $(MODULE_OBJS) $(LIBRADIUS)
	$(LIBTOOL) --mode=link --tag=CC $(CC) -export-dynamic -dlopen self \
		$(LDFLAGS) $(LINK_MODE) -o $@ $(SERVER_OBJS)      \
		$(MODULE_LIBS) $(LIBRADIUS) $(LIBS) $(SNMP_LIBS) \
		$(LCRYPT) $(PTHREADLIB) $(LIBLTDL)

radiusd.lo: radiusd.c  ../include/modules.h ../include/modcall.h ../include/modpriv.h
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c radiusd.c

%.lo: %.c
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS)  -c $<

acct.lo: acct.c ../include/modules.h
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c acct.c

auth.lo: auth.c ../include/modules.h
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c auth.c

client.lo: client.c ../include/conffile.h
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c client.c

conffile.lo: conffile.c ../include/conffile.h ../include/modules.h
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c conffile.c

mainconfig.lo: mainconfig.c ../include/modules.h
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c mainconfig.c

modcall.lo: modcall.c
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c modcall.c

modules.lo: modules.c ../include/modules.h
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) $(VFLAGS) -c modules.c

session.lo: session.c ../include/modules.h
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c session.c

# It's #include'd for simplicity.  This should be fixed...
listen.lo: listen.c dhcpd.c command.c
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c listen.c

#
# Helper programs
#

radclient.lo: radclient.c $(INCLUDES)
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -I ../modules/rlm_mschap -c radclient.c

MSCHAP_OBJS := ../modules/rlm_mschap/smbdes.lo ../modules/rlm_mschap/mschap.lo 

radclient$(EXEEXT): radclient.lo $(MSCHAP_OBJS) $(LIBRADIUS)
	$(LIBTOOL) --mode=link --tag=CC $(CC) $(LDFLAGS) $(LINK_MODE) -o radclient radclient.lo $(MSCHAP_OBJS) $(LIBRADIUS) $(LIBS)

# These two rules need to be specific in order to supercede the generic
# "compile C file" rules.
../modules/rlm_mschap/smbdes.lo: ../modules/rlm_mschap/smbdes.c
	${MAKE} -C ../modules/rlm_mschap/

../modules/rlm_mschap/mschap.lo: ../modules/rlm_mschap/mschap.c
	${MAKE} -C ../modules/rlm_mschap/

radsniff.lo: radsniff.c $(INCLUDES) ../include/radsniff.h
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c radsniff.c

radsniff$(EXEEXT): radsniff.lo $(LIBRADIUS)
	$(LIBTOOL) --mode=link --tag=CC $(CC) $(LDFLAGS) $(LINK_MODE) -o radsniff radsniff.lo $(LIBRADIUS) $(LIBS) $(PCAP_LIBS)

radwho.lo: radwho.c $(INCLUDES)
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c radwho.c

radwho$(EXEEXT): radwho.lo util.lo log.lo conffile.lo $(LIBRADIUS)
	$(LIBTOOL) --mode=link --tag=CC $(CC) $(LDFLAGS) $(LINK_MODE) -o radwho radwho.lo util.lo log.lo conffile.lo $(LIBRADIUS) $(LIBS)

radmin$(EXEEXT): radmin.lo $(LIBRADIUS) util.lo log.lo conffile.lo
	$(LIBTOOL) --mode=link --tag=CC $(CC) $(LDFLAGS) $(LINK_MODE) -o $@ $^ $(LIBREADLINE) $(LIBS)

radconf2xml$(EXEEXT): radconf2xml.lo $(LIBRADIUS) util.lo log.lo conffile.lo
	$(LIBTOOL) --mode=link --tag=CC $(CC) $(LDFLAGS) $(LINK_MODE) -o $@ $^ $(LIBS)

dhcpclient.lo: dhcpclient.c $(INCLUDES)
	$(LIBTOOL) --mode=compile --tag=CC $(CC) $(CFLAGS) -c dhcpclient.c

# Don't install this for now.
dhcpclient$(EXEEXT): dhcpclient.lo $(LIBRADIUS)
	$(LIBTOOL) --mode=link --tag=CC $(CC) $(LDFLAGS) $(LINK_MODE) -o dhcpclient dhcpclient.lo $(LIBRADIUS) $(LIBS)

clean:
	rm -rf *.o *.so *.lo *~ $(BINARIES) .libs

install:
	$(INSTALL) -d -m 755	$(R)$(sbindir)
	$(INSTALL) -d -m 755	$(R)$(bindir)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radiusd$(EXEEXT)	$(R)$(sbindir)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radmin$(EXEEXT)	$(R)$(sbindir)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radclient$(EXEEXT)	$(R)$(bindir)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radwho$(EXEEXT)	$(R)$(bindir)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radconf2xml$(EXEEXT)	$(R)$(bindir)
ifneq ($(PCAP_LIBS),)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radsniff$(EXEEXT)	$(R)$(bindir)
endif
	$(INSTALL) -m 755    checkrad.pl		$(R)$(sbindir)/checkrad
	$(INSTALL) -m 755    radlast			$(R)$(bindir)
	$(INSTALL) -m 755    radtest 			$(R)$(bindir)
	$(INSTALL) -m 755    radzap			$(R)$(bindir)

reconfig:
